package com.masita.util;

import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Cifrador {
	
	private static Cifrador cifrador = null;
	
	private Cifrador(){
		super();
	}
	
	/**
	 * 
	 * @return
	 */
	public static Cifrador getInstance(){
		if( cifrador == null )
			cifrador = new Cifrador();
		
		return cifrador;
	}
	
	/**
	 * 
	 * @param cadena
	 * @return
	 */
	public String codificar(String cadena){
		BASE64Encoder enc = new BASE64Encoder();
		return enc.encode(cadena.getBytes());
	}

	/**
	 * 
	 * @param cadena
	 * @return
	 */
	public String decodificar(String cadenaB64){
		try {
			BASE64Decoder dec = new BASE64Decoder();
			return new String(dec.decodeBuffer(cadenaB64));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param cadena
	 * @param cadenaB64
	 * @return
	 */
	public boolean esIgual(String cadena, String cadenaB64){
		BASE64Encoder enc = new BASE64Encoder();
		return enc.encode(cadena.getBytes()).equals(cadenaB64);
	}

}
